import { DrawerBase } from "./DrawerBase"
import type { EventsConfig } from "./DrawerBase"
import * as Cesium from "cesium"
import { generateArrowPolygon } from "../../../utils"

export class ArrowDrawer extends DrawerBase {
  protected events: EventsConfig = {
    cesiumEvents: [
      {
        type: "click",
        callback: (e: Cesium.ScreenSpaceEventHandler.PositionedEvent) => {
          const position = this.scene.pickPosition(e.position)
          position && this.featurePositions.value.push(position)
          if (this.featurePositions.value.length === 2) {
            //绘制箭头
            const tempPoints = this.featurePositions.value.map((item) =>
              item.clone(),
            )
            this.drawFeature({
              polygon: {
                hierarchy: new Cesium.PolygonHierarchy(
                  generateArrowPolygon(tempPoints[0], tempPoints[1]),
                ),
                ...this.styleOption?.arrow,
              },
            })

            //清空顶点
            this.featurePositions.value = []
          }
        },
      },
    ],
  }
  protected helpers: Cesium.Entity.ConstructorOptions[] = [
    // 鼠标位置
    {
      position: new Cesium.CallbackPositionProperty(() => {
        return this.mousePosition
      }, false),
      point: {},
    },
    // 动态箭头
    {
      polygon: {
        hierarchy: new Cesium.CallbackProperty(() => {
          if (this.mousePosition && this.featurePositions.value.length === 1) {
            const startPoint = this.featurePositions.value[0].clone()
            const endPoint = this.mousePosition.clone()
            return new Cesium.PolygonHierarchy(
              generateArrowPolygon(startPoint, endPoint),
            )
          }
          return undefined
        }, false),
      },
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          if (this.mousePosition && this.featurePositions.value.length === 1) {
            const tempPositions = generateArrowPolygon(
              this.featurePositions.value[0].clone(),
              this.mousePosition.clone(),
            )
            return [...tempPositions, tempPositions[0]]
          }
          return undefined
        }, false),
        clampToGround: true,
      },
    },
  ]
}
